Repository navigation
drm/asahi: g16: add a recovery PB slot for a render accepted before a hang - #4240
Conversation
… hang Each rendering VM gets two warm PB/UMA allocator slots, and the next VM's home slot is its alternate. When a render of the next VM hangs on that shared slot, its abort quarantines the slot for good, and the recovery makes every VM start a fresh InitBM epoch, which may only go on a displaced slot. A VM alone whose render was accepted before the hang then has none: its own slot was the last one the TA and FS engines ran, the other one is quarantined, and recovery slots (WARM_PB_SLOTS..PB_SLOTS) were only added at SUBMIT. The engine cancelled that render with ECANCELED and the innocent client saw a lost device. Keep the VM's recovery slots in its cache behind a spinlock the engine may take, and give a prepared job set-once cells for them. When the drained engine finds no slot a render can take, the job takes the recovery slots its VM got since the job was prepared. If there are none, a one-shot worker adds one off the device lock and wakes publication while the engine holds the render. The render is cancelled as before only when no slot can be added (allocation failure, every recovery slot quarantined) or when it is an SCS render, which uses base slots only. A strand leaves the VM at most one healthy slot (of two healthy idle slots one is always displaced), so the worker grows to the same BASE_PB_SLOTS target as SUBMIT, through one shared helper. Add tools/asahi/g16-late-pb-spare-tests.py: the production slot picker and quarantine bookkeeping (pools.rs) and the new resources.rs code, built against host mocks, replay the hang next to a queued render. It fails on the previous engine decision and kills four mutants. Update the asahi-m4 resources and publication harnesses for the set-once cells and the hold. Signed-off-by: DjDeveloperr <hey@djdev.me>
Cache::close() took the recovery PB slots in the tail expression of the block that holds the ready lock, so the spinlock guard, a temporary of that expression, outlived the ready guard. Take them in a statement of their own, so the spinlock is released before the ready lock, in the reverse order of taking them. Signed-off-by: DjDeveloperr <hey@djdev.me>
Cache::add_late_pb() takes the ready lock, and a render the engine holds for a recovery PB slot waits for that worker, so the comment in Cache::prepare() that counts fence signalling among the paths that never hold the lock is no longer true. Say instead that no GPU wait may go under it: one would leave the held render, and publication with it, waiting for itself. The comment keeps its two lines, so the built module does not change. Signed-off-by: DjDeveloperr <hey@djdev.me>
There was a problem hiding this comment.
One review round at 5a9369be0947, plus one small fix pushed on top (812188767bea, comment only).
Verdict: the fix is right and small. It can merge once the J773g and J516s rows pass (both module-only row kits are sealed at this head). This is a comment review: the PR is on my own account.
What I checked
- Lock order. The new
pb_extspinlock is always taken last: underreadyinprepare(),grow_pb_ext()andclose(), and under the device lock inpb_stranded(). Nothing is locked, allocated or freed while it is held. The slotgrow_pb_ext()fills is always empty because every writer holdsready, and the second commit makesclose()release it beforeready. The engine never takesready, andclose()is not called under the device lock. So no lock cycle. - Fence path. The engine side never sleeps or reclaims (
try_spawn(GFP_NOWAIT),SetOnce::populate, anArcclone). The worker'sGFP_KERNELallocation can't wait on the held render: the driver's only shrinker (VmShrinker) frees idle VMs throughGate::try_reclaimand never waits for the GPU. - Teardown. The worker owns an
Arc<Cache>and anArc<VmState>.close()setsclosedbefore takingready, and the worker checksclosed, the VM status andis_closed()underready. So a slot added just before a close is taken out and dropped byclose(). A failed VM leaves the state at FAILED, and no render of that VM comes back to read it. - Second recovery while the worker runs. It can quarantine the slot just added. The render adopts that slot, is still stranded and asks again. Each round adds another of the four recovery slots, then the render is cancelled, so the loop ends.
- Can a held render stall publication for good? A failed spawn, a failed allocation, a closed or failed VM, or all four recovery slots quarantined each end in the same cancel as on main. The worker only waits for one allocation and for
ready, andreadyis only held across allocations. The hold has no deadline of its own, and I don't think it needs one. - SCS.
pb_stranded()returnsENOSPCbefore touching the state, so SCS renders are cancelled as on main. - Worse than main? Only renders that main would cancel take the new path. The cost is that publication stops behind the held render while the worker runs (the engine
breaks on it), for every VM, not only the stranded one. That is one allocation, and only after a recovery, so it's fine. - Org rules: no module parameter, runtime switch or SoC gate. The
LATE_PB_*constants are names for states.
Fixed in 812188767bea. prepare() said ready is never held by fence signalling. That stops being true here: a held render, and publication behind it, waits for add_late_pb(), which takes ready. A GPU wait added under ready later would now deadlock the device, so the comment says that instead. It keeps its two lines, so the built module is byte-identical to the one from 5a9369be0947 (checked on the J773g and J516s kernels).
Tests at 812188767bea (pinned ubuntu 24.04 container, rust 1.96):
g16-late-pb-spare-tests.py:PASS control, 4/4 mutants killed. With--rootat main7303d70ea68dit reportsFAIL line 1, as the PR says.run-host-tests.py --only g16: 4/4.--only asahi-m4: 1/1 (test-resources 102 passed, test-publication 94 passed).
Still owed before merge: the J773g row and the J516s row (the g16 backend is shared with M3). Both passed, see below.
Update 00:48Z, a second read by a reviewer that didn't push (same head 812188767bea). The rule is one review per PR, so this goes here and not in a new review.
-
Rows (comments above): J773g (Mac mini M4, G16G) PASS: base cancelled the queued client in 4 of 6 rounds, fix in 0 of 6, and the worker added the slot. J516s (M3 Pro, G15S) PASS: base 3 of 6, fix 6 of 6 with the strand hit in 4 rounds, WARN=0.
-
Code, read again without the notes above: the device lock is a mutex, so
pb_extis a leaf spinlock under it and underready. Nothing is freed while it is held: the cache keeps its own reference, so an adopted clone is never the last one, andclose()drops the taken slots after both locks.SetOncedrops what it holds, so an adopted slot goes away with its job. A new recovery slot has no owner, so the picker counts it as displaced and the held render can take it. Onlyclose(),prepare()and the worker takeready, and none of them waits on the GPU under it.close()setsclosedbefore it takesready, so a late worker can't put a slot back. Only the g16 backend runs it (M4, and M3 on 27.2 firmware); the g15 and g17 backends don't. -
Not blocking: if a held render's queue goes away before the engine reads a failed addition, the VM's next strand is cancelled without a new try. Main cancels it too, so this is no worse.
-
Local checks on the exact head (pc: merge base
7303d70e+ this diff, the six changed files checked against the head's blob ids). The asahi crate type-checks with a warm J516s tree's own rustc line: 41 warnings on base and on head, the same set. Withclippy-driver(whatCLIPPY=1runs) the set is also unchanged; oneneedless_range_loopmoved with its loop. rustfmt finds nothing in the new code. In the pinned host-test container:run-host-tests.py --only g164/4,--only asahi-m41/1,g16-late-pb-spare-tests.pycontrol PASS with 4/4 mutants killed, and it FAILs at the merge base. -
CI: apple-build, host-tests, KUnit asahi, readiness and pr-text all pass at this head.
No changes needed. It's ready for an approval from someone who didn't push it.
|
J773g (Mac mini M4, T8132, G16G B0, OS fw 27.2) A/B hardware verification for head
|
|
J516s (MacBook Pro 14" M3 Pro,
|
|
Merged at |
What: After one client's render hangs and the GPU recovers, another client's render that was already queued could be cancelled (
ECANCELED, the client sees a lost device). The hang had quarantined the PB allocator slot its VM shared with the hung VM, and recovery slots were only added at SUBMIT. The engine now has a recovery slot added for such a render off the device lock and holds it meanwhile; it cancels only when no slot can be added. Refs #4032 (its hang rows found this).Tested: J773g (Mac mini M4) and J516s (M3 Pro) rows PASS at
812188767bea(comments below): the queued client was cancelled in 4 of 6 and 3 of 6 rounds on base, 0 of 6 on both with the fix. Host: W=1 ofdrivers/gpu/drm/asahibuilds with the same 42 warnings as main.tools/asahi/run-host-tests.py --only g164/4 and--only asahi-m41/1 in the pinned ubuntu 24.04 container (rust 1.96). The newg16-late-pb-spare-tests.pyfails on main's code and kills 4/4 mutants.Risk: two-way · M4 (T8132) and M3, M3 Pro, M3 Max (T8122, T6030, T6031) on 27.2 firmware, the Macs that use the g16 backend. Only a render left with no PB slot it can take after a GPU recovery goes the new way.
Not yet: SCS renders in that state are still cancelled: they use base slots only.
Details
How the render gets stranded. Each rendering VM gets two warm PB/UMA allocator slots
[home, alt]at its first render (pick_base_pb_slots(next_pb_slot++)), so VM n holds {n, n+1} and the next VM's home is its alt. A render of that next VM that hangs on its home has its abort quarantine the slot for good (Pools::quarantine_flight). Every recovery callsPools::invalidate(), so each VM's next render needs a fresh InitBM epoch, which may only go on a displaced slot. The queued VM's own slot was the last one the TA and FS engines ran, so it is not displaced, and its other slot is quarantined. The recovery slots (WARM_PB_SLOTS..PB_SLOTS) were added only inCache::prepare(), and this render was prepared before the hang, soexhausted_for_mask_modewas true and the engine cancelled it. With many clients another render displaces the slot, so the case needs a lone client next to the hung one.The change.
Ready(its mutex is never taken by publication) to a spinlock onCachethat the engine may take. A prepared job keeps them in set-once cells instead of a snapshot.Prepared::pb_strandedgives the job the recovery slots its VM got since the job was prepared. If there are none, it spawns a one-shot worker (system_unbound, the spawn itselfGFP_NOWAIT) and the engine holds the render. The worker adds a slot underready, through the samegrow_pb_exthelper SUBMIT uses, and wakes publication.BASE_PB_SLOTStarget as SUBMIT.Why not a recovery slot for every VM up front. Each recovery slot carries a 32 MiB UMA pool, per VM, and SCS renders would still be exposed.
Fence path. The held render's fence now waits for one allocation, done in a worker outside the device lock and the fence signalling section, as G17 grows render PB memory in a worker today. The engine stops publishing for that one allocation, which only happens after a recovery.
No module parameter, runtime switch or SoC gate. G17 has its own render resource code and does not take this path.
Evidence
tools/asahi/g16-late-pb-spare-tests.py: copies the slot picker, publication, retirement, quarantine and recovery bookkeeping fromg16/pools.rsand the newg16/resources.rscode into a host build with mocks, and replays the hang next to a queued render (plus an SCS render, an allocation failure and every recovery slot quarantined). It also checks thatpublish_checkedinengine.rskeeps the decision the test models.PASS control,4/4 mutants killed(cancel-stranded, no-adopt, failed-holds, respawn)--rootat main 7303d70:FAIL line 1: the render queued before the hang was cancelledtest-resources.py(102 passed) andtest-publication.py(94 passed, newstranded_render_waits_for_its_recovery_slot) updated for the set-once cells and the hold.drivers/gpu/drm/asahi/(arm64, gcc + rustc) from a warm tree: OK on main and on this branch, identical 42 warnings, none in the changed code.